﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LaoLuo
{
    public static class MathHelper
    {
        //public static TOut Ceiling<TIn, TOut>(TIn value)
        //    where TIn : struct, IComparable, IFormattable, IConvertible
        //    where TOut : struct, IComparable, IFormattable, IConvertible
        //{
        //    ThrowHelper.ThrowIfNotNumericType<TIn>();
        //    ThrowHelper.ThrowIfNotNumericType<TOut>();

        //    return TypeConverter.ToType<TOut>(Math.Ceiling(Convert.ToDecimal(value)));
        //}

        public static int QuotientCeiling(int a, int b)
        {
            int rem;

            return Math.DivRem(a, b, out rem) + (rem > 0 ? 1 : 0);
        }

        public static long QuotientCeiling(long a, long b)
        {
            long rem;

            return Math.DivRem(a, b, out rem) + (rem > 0 ? 1 : 0);
        }

        public static T RoundQuotient<T>(T a, T b)
            where T : IConvertible
        {
            return HandleQuotient(a, b, Math.Round);
        }

        public static T HandleQuotient<T>(T a, T b, Func<decimal, decimal> handler)
            where T : IConvertible
        {
            return TypeConverter.ToType<T>(handler(Convert.ToDecimal(a) / Convert.ToDecimal(b)));
        }
    }
}
